package com.yuehuanghun.admin.core.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.domain.SysNotice;
import com.yuehuanghun.admin.core.service.ISysNoticeService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.web.BaseResource;
import com.yuehuanghun.mybatis.milu.data.Sort;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 公告 信息操作处理
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/notice")
@SaCheckLogin
public class SysNoticeController extends BaseResource {
	@Autowired
	private ISysNoticeService noticeService;

	/**
	 * 获取通知公告列表
	 */
	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	@SaCheckPermission("system:notice:list")
	public PageDTO<SysNotice> list(SysNotice notice) {
		List<SysNotice> list = noticeService.getByExampleAndSort(notice, Sort.desc(SysNotice::getNoticeId), getPage());
		return toPage(list);
	}

	/**
	 * 根据通知公告编号获取详细信息
	 */
	@GetMapping(value = "/{noticeId}")
	@SaCheckPermission("system:notice:query")
	public SysNotice getInfo(@PathVariable Long noticeId) {
		return noticeService.getById(noticeId).orElse(null);
	}

	/**
	 * 新增通知公告
	 */
	@Log(title = "通知公告", businessType = BusinessType.INSERT)
	@PostMapping
	@SaCheckPermission("system:notice:add")
	public void add(@Validated @RequestBody SysNotice notice) {
		noticeService.add(notice);
	}

	/**
	 * 修改通知公告
	 */
	@Log(title = "通知公告", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("system:notice:edit")
	public void edit(@Validated @RequestBody SysNotice notice) {
		noticeService.updateById(notice);
	}

	/**
	 * 删除通知公告
	 */
	@Log(title = "通知公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
	@SaCheckPermission("system:notice:remove")
	public void remove(@PathVariable List<Long> noticeIds) {
		noticeService.deleteByIds(noticeIds);
	}
}
